package net.shopxx.dao.impl;

import java.util.Collections;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import net.shopxx.dao.TaskDao;
import net.shopxx.entity.Member;
import net.shopxx.entity.Task;
import net.shopxx.pager.Page;
import net.shopxx.pager.Pageable;

/**
 * Dao - 任务
 * 
 * @author frankie
 * @version 3.0
 */
@Repository("taskDaoImpl")
public class TaskDaoImpl extends BaseDaoImpl<Task, Long> implements TaskDao {
	
	public Page<Task> findPage(Member member, Pageable pageable, boolean taskPublisher) {
		if (member == null) {
			return new Page<Task>(Collections.<Task> emptyList(), 0, pageable);
		}
		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
		CriteriaQuery<Task> criteriaQuery = criteriaBuilder.createQuery(Task.class);
		Root<Task> root = criteriaQuery.from(Task.class);
		criteriaQuery.select(root);
		if(taskPublisher){
			criteriaQuery.where(criteriaBuilder.equal(root.get("publisher"), member));
		}else{
			criteriaQuery.where(criteriaBuilder.equal(root.get("operator"), member));
		}
		
		return super.findPage(criteriaQuery, pageable);
	}

}
